package com.cn.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.hibernate.Session;

import com.cn.domain.BigCart;
import com.cn.domain.Book;
import com.cn.domain.Cart;
import com.cn.service.BookService;
import com.cn.service.CartService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class CartAction extends ActionSupport implements ModelDriven<Book> {

	private CartService cartService;
	/*
	 * 注入BookService bookService
	 */
	private BookService bookService;
	/*
	 * 封装表单传过来checkbox
	 */
	private String choice;
	private Book model = new Book();
	/*
	 * 封装页面传的数量 number
	 */
	private int counts;

	public String getChoice() {
		return choice;
	}

	public void setChoice(String choice) {
		this.choice = choice;
	}

	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	public int getCounts() {
		return counts;
	}

	public void setCounts(int counts) {
		this.counts = counts;
	}

	@Override
	public Book getModel() {

		return model;
	}

	public void setCartService(CartService cartService) {
		this.cartService = cartService;
	}

	/*
	 * 购物车页面
	 */
	public String addCartPage() {

		getBigCart();
		return "cartpage";
	}

	/*
	 * 添加到购物车功能
	 */
	public String addBooks() {
		// 判断session中有没有用户 每用户跳转到登陆界面
		String exitcustomer = (String) ActionContext.getContext().getSession()
				.get("exitcustomer");
		if (exitcustomer == null)
			// 返回到登陆界面
			return "loginpage";
		// 跳转到购物车页面
		/*
		 * 封装表单提交的图书的信息
		 */
		else {
			Book book = bookService.getBookId(model.getBid());
			int Number = this.getCounts();// 得到选择该商品的数量
			// 购物车的一条记录
			Cart cart = new Cart();
			cart.setBook(book);// 封装书的信息
			cart.setNumber(Number);// 封装书的个数

			// 封装购物车BigCart
			/*
			 * System.out.println("carts"+cart.getNumber());
			 * System.out.println("carts"+cart.getBook().getBname());
			 */

			BigCart bigCart = getBigCart();
			System.out.println("bigCart=" + bigCart);
			bigCart.addBigCart(cart);

			return "cartpage";
		}
	}

	/*
	 * 将购物车存到Session里边
	 */
	private BigCart getBigCart() {
		BigCart exitsbigCart = (BigCart) ActionContext.getContext()
				.getSession().get("exitsbigCart");
		// addSessionId();

		if (exitsbigCart == null) {
			// 新建一个购物车
			BigCart newBigCart = new BigCart();
			// 将新建的购物车放入session中
			// ActionContext.getContext().getSession().put("exitsbigCart",
			// newBigCart);
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
			session.put("exitsbigCart", newBigCart);
			// addSessionId();
			return newBigCart;
		}
		return exitsbigCart;
	}

	/*
	 * 将cookie存为session
	 */public void addSessionId() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String sessionid = request.getSession().getId();
		System.out.println("sessionid=" + sessionid);
		// 创建Cookie并且保存sessionid到cookie中去
		Cookie cookie = new Cookie("sessionid", sessionid);
		cookie.setMaxAge(60 * 60 * 24 * 365);
		cookie.setPath("/");
		ServletActionContext.getResponse().addCookie(cookie);
	}

	/*
	 * 修改购物车的信息
	 */
	public String updateBigCart() {
		/*
		 * 修改购物车的数量，将金额显示到页面
		 */
		// 1.获取购物车提交的书的件数 后台传值一个数组 因为显得可能不是一本书
		System.out.println("新提交的书" + this.getCounts());
		return null;
	}

	/*
	 * 删除cart
	 */
	public String deleteCart() {
		// 后台进来一个cid
		BigCart _bigCart = (BigCart) ActionContext.getContext().getSession()
				.get("exitsbigCart");
		Map<Integer, Cart> _newcart = _bigCart.deleteCart(model.getBid());// 购物车的一条记录,得到新的购物车的集合
		/*
		 * for(Integer key:_newcart.keySet()){
		 * System.out.println("key====="+key+
		 * "value========="+_newcart.get(key).getNumber()); }
		 */

		// 然后封装成购物车存到session里边
		_bigCart.setCartmap(_newcart);

		ActionContext.getContext().getSession().put("exitsbigCart", _bigCart);
		return "cartpage";
	}

}
